package com.liuwei.netty.packet;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;

public class NettyMessageClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 200; i++) {
            String msg = "擎天白玉柱";
            // 创建消息协议对象
            MessageProtocol protocol = new MessageProtocol();
            // 封装长度和内容
            byte[] content = msg.getBytes(StandardCharsets.UTF_8);
            protocol.setContent(content);
            protocol.setLength(content.length);

            // 发送协议对象
            ctx.writeAndFlush(protocol);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {

    }
}
